package com.example.ccj.b.f;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * fragment基类
 * 2024年5月13日00:17:21
 */
public class BaseFragment extends Fragment {
    protected Context context;
    public BaseFragment(){}

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.context = getContext();
//        offsetStatusBar(view);
    }

    /**
     * 将布局偏移出状态栏
     * @param view 当前fragment布局
     */
    private void offsetStatusBar(@NonNull View view){
        //如果fragment属于activity则偏移出状态栏的高度
        //预留出状态栏高度
        if (getActivity() != null) {
            ViewGroup winContent =  getActivity().findViewById(android.R.id.content);
            if (winContent.getChildCount() > 0) {
                ViewGroup rootView = (ViewGroup) winContent.getChildAt(0);
                if (rootView != null) {
                    boolean fitsSystemWindows = rootView.getFitsSystemWindows();
                    //判断当前activity预留状态栏的状态，如果没有预留出来，将布局偏移
                    if (!fitsSystemWindows) {
                        //获取状态栏高度
                        int resourceId = getActivity().getApplicationContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
                        if (resourceId > 0) {
                            int height = getActivity().getApplicationContext().getResources().getDimensionPixelSize(resourceId);
                            //将布局顶部偏移出状态栏的高度
                            view.setPadding(0, height,0,0);
                        }
                    }
                }
            }
        }
    }

}
